package com.hu.zhcc.shiro.controller.xy;

import com.alibaba.fastjson.JSONObject;
import com.hu.zhcc.common.entity.Page;
import com.hu.zhcc.shiro.entity.xy.dto.TMResultDTO;
import com.hu.zhcc.shiro.entity.xy.pojo.TAssessQuestion;
import com.hu.zhcc.shiro.entity.xy.vo.TBodyTypeListVO;
import com.hu.zhcc.shiro.entity.xy.vo.TMResultDetailVO;
import com.hu.zhcc.shiro.entity.xy.vo.TMResultListVO;
import com.hu.zhcc.shiro.service.xy.TMResultService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/tMResult")
public class TMentalityResultController {

    @Autowired
    private TMResultService service;


    @GetMapping("")
    public ResponseEntity<Page<TMResultListVO>> getTMResultList(@RequestParam(required = false) String name,
                                                            int offset, int limit) {
        Map<String, Object> paremeters = new HashMap<String, Object>();
        if (!StringUtils.isBlank(name)) {
            paremeters.put("name", name);
//            paremeters.put("memberId", name);
        }
        System.out.println(paremeters);
        Page<TMResultDTO> result = this.service.listTMResult(paremeters, offset, limit);
        List<TMResultListVO> voList = new ArrayList<TMResultListVO>();

        if(result.getRows()==null){
            return ResponseEntity.ok(new Page<TMResultListVO>(result.getTotal(), voList));
        }
        for(TMResultDTO dto : result.getRows()) {
            TMResultListVO vo = new TMResultListVO();
            BeanUtils.copyProperties(dto, vo);
            vo.setCtime(vo.getCreatetime());
            System.out.println(vo.toString());
            voList.add(vo);
        }
        return ResponseEntity.ok(new Page<TMResultListVO>(result.getTotal(), voList));
    }

    @PostMapping("")
    public ResponseEntity<TMResultDetailVO> saveTMResult(@RequestBody TMResultDetailVO vo) {
            TMResultDTO dto = new TMResultDTO();
        BeanUtils.copyProperties(vo, dto);
        this.service.insertSelective(dto);
        vo.setId(dto.getId());

        return ResponseEntity.status(HttpStatus.CREATED).body(vo);
    }
//
    @GetMapping("/{id}")
    public ResponseEntity<TMResultDetailVO> getById(@PathVariable("id") int id) {
        TMResultDTO dto = this.service.selectByPrimaryKey(id);
        if(dto == null) {
            return ResponseEntity.notFound().build();
        }

        TMResultDetailVO vo = new TMResultDetailVO();
        BeanUtils.copyProperties(dto, vo);
        return ResponseEntity.ok(vo);
    }
//
    @PutMapping("/{id}")
    public ResponseEntity<Integer> updateTMResult(@PathVariable("id") int id, @RequestBody TMResultDetailVO vo) {
        TMResultDTO dto = new TMResultDTO();
        BeanUtils.copyProperties(vo, dto);
        int rows = this.service.updateByPrimaryKeySelective(dto);
        return rows == 0 ? ResponseEntity.notFound().build() :
                ResponseEntity.status(HttpStatus.CREATED).body(rows);
    }
//
    @GetMapping("/all")
    public ResponseEntity<List<TMResultListVO>> listAllTMResult() {
        List<TMResultDTO> dtoList = this.service.listAllTMResult();
        if(dtoList.size() == 0) {
            return ResponseEntity.notFound().build();
        }
        List<TMResultListVO> voList = new ArrayList<TMResultListVO>(dtoList.size());
        for(TMResultDTO dto : dtoList) {
            TMResultListVO vo = new TMResultListVO();
            BeanUtils.copyProperties(dto, vo);
            voList.add(vo);
        }
        return ResponseEntity.ok(voList);
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<?> removeTMResult(@PathVariable("id") int id) {
        return this.service.deleteByPrimaryKey(id) > 0 ?
                ResponseEntity.noContent().build() : ResponseEntity.notFound().build();
    }

    @PutMapping("/updateResult/{id}")
    public Integer updateTBodyType(@PathVariable("id") int id, @RequestBody String answers) {
//        System.out.println(id);
        TAssessQuestion[][] objects = JSONObject.parseObject(answers, TAssessQuestion[][].class);
        return service.updateTMentalityResult(id,objects);
    }

    @PostMapping("/addResult/{idCard}")
    public Integer addTBodyType(@PathVariable("idCard") String idCard, @RequestBody String answers){
        TAssessQuestion[][] objects = JSONObject.parseObject(answers, TAssessQuestion[][].class);
        return service.addTMentalityResult(idCard,objects);
    }



}